package com.atguigu07.object.tostring;

/**
 * ClassName: ToStringTest
 * Package: com.atguigu07.object.tostring
 * Description:
 *
 * @Author 姜
 * @Create 2023/11/22 17:20
 * @Version 1.0
 */
public class ToStringTest {
    public static void main(String[] args) {
        User u1 = new User("法师", 25);
        System.out.println(u1.toString()); // com.atguigu07.object.tostring.User@7cca494b

        String s1 = new String("你好");
        System.out.println(s1.toString());
    }
}

class User {
    String name;
    int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 手动实现toString()
//    @Override
//    public String toString() {
//        return "User{ name = " + name + ", age = " + age + "}";
//    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
